home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / multiprocessing / reduction.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  6KB  |  165 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __all__ = []
  5. import os
  6. import sys
  7. import socket
  8. import threading
  9. import _multiprocessing
  10. from multiprocessing import current_process
  11. from multiprocessing.forking import Popen, duplicate, close, ForkingPickler
  12. from multiprocessing.util import register_after_fork, debug, sub_debug
  13. from multiprocessing.connection import Client, Listener
  14. if not sys.platform == 'win32' or hasattr(_multiprocessing, 'recvfd'):
  15.     raise ImportError('pickling of connections not supported')
  16. hasattr(_multiprocessing, 'recvfd')
  17. if sys.platform == 'win32':
  18.     import _subprocess
  19.     from _multiprocessing import win32
  20.     
  21.     def send_handle(conn, handle, destination_pid):
  22.         process_handle = win32.OpenProcess(win32.PROCESS_ALL_ACCESS, False, destination_pid)
  23.         
  24.         try:
  25.             new_handle = duplicate(handle, process_handle)
  26.             conn.send(new_handle)
  27.         finally:
  28.             close(process_handle)
  29.  
  30.  
  31.     
  32.     def recv_handle(conn):
  33.         return conn.recv()
  34.  
  35. else:
  36.     
  37.     def send_handle(conn, handle, destination_pid):
  38.         _multiprocessing.sendfd(conn.fileno(), handle)
  39.  
  40.     
  41.     def recv_handle(conn):
  42.         return _multiprocessing.recvfd(conn.fileno())
  43.  
  44. _cache = set()
  45.  
  46. def _reset(obj):
  47.     global _lock, _listener
  48.     for h in _cache:
  49.         close(h)
  50.     
  51.     _cache.clear()
  52.     _lock = threading.Lock()
  53.     _listener = None
  54.  
  55. _reset(None)
  56. register_after_fork(_reset, _reset)
  57.  
  58. def _get_listener():
  59.     global _listener
  60.     if _listener is None:
  61.         _lock.acquire()
  62.         
  63.         try:
  64.             if _listener is None:
  65.                 debug('starting listener and thread for sending handles')
  66.                 _listener = Listener(authkey = current_process().authkey)
  67.                 t = threading.Thread(target = _serve)
  68.                 t.daemon = True
  69.                 t.start()
  70.         finally:
  71.             _lock.release()
  72.  
  73.     
  74.     return _listener
  75.  
  76.  
  77. def _serve():
  78.     is_exiting = is_exiting
  79.     sub_warning = sub_warning
  80.     import util
  81.     while None:
  82.         
  83.         try:
  84.             conn = _listener.accept()
  85.             (handle_wanted, destination_pid) = conn.recv()
  86.             _cache.remove(handle_wanted)
  87.             send_handle(conn, handle_wanted, destination_pid)
  88.             close(handle_wanted)
  89.             conn.close()
  90.         continue
  91.         if not is_exiting():
  92.             import traceback as traceback
  93.             sub_warning('thread for sharing handles raised exception :\n' + '-' * 79 + '\n' + traceback.format_exc() + '-' * 79)
  94.         
  95.  
  96.         continue
  97.         return None
  98.  
  99.  
  100. def reduce_handle(handle):
  101.     if Popen.thread_is_spawning():
  102.         return (None, Popen.duplicate_for_child(handle), True)
  103.     dup_handle = duplicate(handle)
  104.     _cache.add(dup_handle)
  105.     sub_debug('reducing handle %d', handle)
  106.     return (_get_listener().address, dup_handle, False)
  107.  
  108.  
  109. def rebuild_handle(pickled_data):
  110.     (address, handle, inherited) = pickled_data
  111.     if inherited:
  112.         return handle
  113.     sub_debug('rebuilding handle %d', handle)
  114.     conn = Client(address, authkey = current_process().authkey)
  115.     conn.send((handle, os.getpid()))
  116.     new_handle = recv_handle(conn)
  117.     conn.close()
  118.     return new_handle
  119.  
  120.  
  121. def reduce_connection(conn):
  122.     rh = reduce_handle(conn.fileno())
  123.     return (rebuild_connection, (rh, conn.readable, conn.writable))
  124.  
  125.  
  126. def rebuild_connection(reduced_handle, readable, writable):
  127.     handle = rebuild_handle(reduced_handle)
  128.     return _multiprocessing.Connection(handle, readable = readable, writable = writable)
  129.  
  130. ForkingPickler.register(_multiprocessing.Connection, reduce_connection)
  131.  
  132. def fromfd(fd, family, type_, proto = 0):
  133.     s = socket.fromfd(fd, family, type_, proto)
  134.     if s.__class__ is not socket.socket:
  135.         s = socket.socket(_sock = s)
  136.     
  137.     return s
  138.  
  139.  
  140. def reduce_socket(s):
  141.     reduced_handle = reduce_handle(s.fileno())
  142.     return (rebuild_socket, (reduced_handle, s.family, s.type, s.proto))
  143.  
  144.  
  145. def rebuild_socket(reduced_handle, family, type_, proto):
  146.     fd = rebuild_handle(reduced_handle)
  147.     _sock = fromfd(fd, family, type_, proto)
  148.     close(fd)
  149.     return _sock
  150.  
  151. ForkingPickler.register(socket.socket, reduce_socket)
  152. if sys.platform == 'win32':
  153.     
  154.     def reduce_pipe_connection(conn):
  155.         rh = reduce_handle(conn.fileno())
  156.         return (rebuild_pipe_connection, (rh, conn.readable, conn.writable))
  157.  
  158.     
  159.     def rebuild_pipe_connection(reduced_handle, readable, writable):
  160.         handle = rebuild_handle(reduced_handle)
  161.         return _multiprocessing.PipeConnection(handle, readable = readable, writable = writable)
  162.  
  163.     ForkingPickler.register(_multiprocessing.PipeConnection, reduce_pipe_connection)
  164.  
  165.